package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.entity.OmOrg;
import com.augurit.common.entity.OmOrgAdmin;
import com.augurit.common.system.convert.OmOrgAdminConverter;
import com.augurit.common.system.convert.OmOrgConverter;
import com.augurit.common.system.web.org.form.OmOrgAdminForm;
import com.augurit.common.system.web.org.form.OmOrgForm;

/**
 * 机构管理员注册DAO类
 */
@Repository
public class OmOrgAdminDao extends BaseDao<OmOrgAdmin, Long> {

	public OmOrgAdminForm getForm(Long id){
		if(id != null){
			OmOrgAdmin entity = this.get(id);
			return OmOrgAdminConverter.convertToForm(entity);
		}
		return null;
	}
	
	/**
	 * 判断该用户是否有该机构管理权限
	 * @param userId 用户主键
	 * @param orgId 机构主键
	 * @return bool 返回该用户是否有机构管理权限.如果有权限返回true，没有权限则返回false
	 */
	public boolean isAuthedAdmin(Long orgId, Long userId){
		if(userId != null && orgId != null){
			OmOrgAdmin entity = this.get(userId, orgId);
			if(entity != null)
				return true;
		}
		return false;
	}
	
	/**
	 * 保存机构管理员注册Form对象
	 * @param form 机构管理员注册Form对象
	 */
	public void save(OmOrgAdminForm form){
		
		if(form != null){
			OmOrgAdmin entity = null;
			
			//准备VO对象
			if(form != null && form.getOaId() != null){
				entity = this.get(form.getOaId());
			}else{
				entity = new OmOrgAdmin();
			}
			
			//属性值转换
			OmOrgAdminConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setOaId(entity.getOaId());
		}
	}
	
	/**
	 * 根据机构ID查找记录
	 * @param orgId
	 * @return
	 */
	public List<OmOrgAdmin> getByOrgId(Long orgId){
		if(orgId != null){
			return this.findBy("orgId", orgId);
		}else
			return null;
	}
	
	/**
	 * 删除指定机构ID记录
	 * @param orgId 机构ID
	 */
	public void deleteByOrgId(Long orgId){
		//根据机构ID查找
		List<OmOrgAdmin> entities = getByOrgId(orgId);
		if(entities != null && entities.size() > 0){
			this.delete(entities);
		}
	}
	
	/**
	 * 根据用户ID查找记录
	 * @param userId
	 * @return
	 */
	public List<OmOrgAdmin> getByUserId(Long userId){
		if(userId != null){
			return this.findBy("userId", userId);
		}else
			return null;
	}
	
	/**
	 * 删除指定用户ID记录
	 * @param userId 用户ID
	 */
	public void deleteByUserId(Long userId){
		//根据用户ID查找
		List<OmOrgAdmin> entities = getByUserId(userId);
		if(entities != null && entities.size() > 0){
			this.delete(entities);
		}
	}
	
	/**
	 * 根据指定用户ID获取具有机构管理权限的机构ID列表
	 * @param userId
	 * @return
	 */
	public List<Long> getCanManageOrgIds(Long userId) {
		if(userId != null){
			String hql = "select distinct ps.orgId from OmOrgAdmin ps where ps.userId=?";
			return this.find(hql, userId);
		}else
			return null;
	}
	
	/**
	 * 根据指定用户ID获取具有机构管理权限并且可继续授权的机构ID列表
	 * @param userId
	 * @return
	 */
	public List<Long> getCanManageAndAuthOrgIds(Long userId) {
		if(userId != null){
			String hql = "select distinct ps.orgId from OmOrgAdmin ps where ps.auth='1' and ps.userId=?";
			return this.find(hql, userId);
		}else
			return null;
	}
	
	/**
	 * 根据指定用户ID获取具有机构管理权限并且可继续授权的机构ID列表
	 * @param userId
	 * @return
	 */
	public List<OmOrgForm> getCanManageAndAuthOrgs(Long userId) {
		if(userId != null){
			String hql = "select distinct org from OmOrg org, OmOrgAdmin ps where org.orgId=ps.orgId and ps.auth='1' and ps.userId=?";
			List<OmOrg> list = this.find(hql, userId);
			return OmOrgConverter.convertToForm(list);
		}else
			return null;
	}
	
	/**
	 * 根据指定用户ID获取具有机构管理权限的机构列表
	 * @param userId
	 * @return
	 */
	public List<OmOrgForm> getCanManageOrgs(Long userId) {
		if(userId != null){
			String hql = "select distinct org from OmOrg org, OmOrgAdmin ps where org.orgId=ps.orgId and ps.userId=?";
			List<OmOrg> list = this.find(hql, userId);
			return OmOrgConverter.convertToForm(list);
		}else
			return null;
	}
	
	/**
	 * 根据用户Id和机构Id保存一个OmOrgAdminForm对象
	 * @userId 用户主键
	 * @orgId 机构主键
	 * */
	public void save(Long userId, String auth, Long orgId){
		if(userId != null && orgId != null){
			if(get(userId, orgId) == null){
				OmOrgAdminForm form = new OmOrgAdminForm();
				form.setOrgId(orgId);
				form.setUserId(userId);
				form.setAuth(auth);
				this.save(form);
			}
		}
	}
	
	public void save(Long userId, String auth, Long... orgIds){
		if(orgIds != null){
			for(Long orgId : orgIds){
				save(userId, auth, orgId);
			}
		}
	}
	
	/**
	 * 根据用户id和机构id获取对应的OmOrgAdmin
	 * */
	public OmOrgAdmin get(Long userId, Long orgId){
		if(userId != null && orgId != null){
			
			String hql = "from OmOrgAdmin ps where ps.userId=? and ps.orgId=?";
			List values = new ArrayList();
			values.add(userId);
			values.add(orgId);
			
			OmOrgAdmin entity = this.findUnique(hql, values);
			return entity;
		}
		return null;
	}
	
	/**
	 * 根据机构id和用户id删除对应的OmOrgAdmin
	 * @userId 用户id
	 * @orgId 机构id
	 * */
	public void deleteByUserIdAndOrgId(Long userId, Long orgId){
		if(userId != null && orgId != null){
			OmOrgAdmin entity = this.get(userId,orgId);
			if(entity != null)
				this.delete(entity);
		}
	}
	
	/**
	 * 重新授权用户管理指定机构的权限
	 * @param userId
	 * @param auth
	 * @param newOrgIds
	 */
	public void authNewOrgs(Long userId, String auth, Long... newOrgIds){
		if(userId != null && newOrgIds != null){
			//清除原有的
//			this.deleteByUserId(userId);
			
			//保存新的
			save(userId, auth, newOrgIds);
		}
	}
}
